define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/party/partybranch/index' + location.search,
                    add_url: 'user/party/partybranch/add',
                    edit_url: 'user/party/partybranch/edit',
                    del_url: 'user/party/partybranch/del',
                    multi_url: 'user/party/partybranch/multi',
                    import_url: 'user/party/partybranch/import',
                    table: 'user_party_partybranch',
                }
            });

            var table = $("#table");
            Template.helper("Moment", Moment);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                templateView: true,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'user.nickname', title: __('User_id') },
                        { field: 'type', title: __('Type'), searchList: { "1": __('正式党员'), "2": __('预备党员'), "3": __('党的发展对象'), "4": __('入党积极分子'), "5": __('入党申请人') }, formatter: Table.api.formatter.flag },
                        { field: 'number', title: __('Number') },
                        // { field: 'department.name', title: __('党支部') },
                        // { field: 'thee_department_id', title: __('Thee_department_id') },
                        // { field: 'four_department_id', title: __('Four_department_id') },
                        // { field: 'position_id', title: __('Position_id') },
                        // { field: 'zjoining_time', title: __('Zjoining_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'party_standing', title: __('Party_standing') },
                        // { field: 'educational_level', title: __('Educational_level'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'major', title: __('Major'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'work_unit', title: __('Work_unit'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'job', title: __('Job'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'is_sdy', title: __('Is_sdy'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'is_sdz', title: __('Is_sdz'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'is_officer', title: __('Is_officer'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'actions', title: __('Actions'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'party_application_book', title: __('Party_application_book'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'party_application_time', title: __('Party_application_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'party_active_time', title: __('Party_active_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'study_political_theory', title: __('Study_political_theory'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'party_volunteer_book', title: __('Party_volunteer_book'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        // { field: 'party_volunteer_time', title: __('Party_volunteer_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'party_develop_time', title: __('Party_develop_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'party_member_time', title: __('Party_member_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'preparatory_expiration_time', title: __('Preparatory_expiration_time'), operate: 'RANGE', formatter: Controller.api.formatter.YMD },
                        // { field: 'preparatory_class_training', title: __('Preparatory_class_training'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'create_time', title: __('Create_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime, visible: false },
                        { field: 'update_time', title: __('Update_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime, visible: false },
                        { field: 'status', title: __('Status'), searchList: { "0": __('待审核'), "1": __('通过'), "2": __('未通过') }, formatter: Table.api.formatter.status },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        import: function () {
            $(document).ready(function () {
                // 监听 #c-avatar 输入框的值变化
                $("#c-import_file_url").on("change", function () {
                    var avatarValue = $(this).val(); // 获取输入的值

                    if (avatarValue) {
                        // 发起 AJAX 请求查询数据库
                        $.ajax({
                            url: "ajax/getAvatarData", // 你的后端接口地址
                            type: "GET",
                            data: { avatar: avatarValue }, // 传递 avatar 值
                            dataType: "json",
                            success: function (res) {
                                if (res.code === 1) {
                                    console.log(res)
                                    // 填充查询结果到 #c-import_file_name
                                    $("#c-import_file_name").val(res.data[0].filename);
                                } else {
                                    console.error("查询失败", res.msg);
                                }
                            },
                            error: function () {
                                console.error("请求失败");
                            }
                        });
                    }
                });
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                YMD: function (value, row, index) {
                    // 检查值是否存在
                    if (value) {
                        // 将时间戳转换为日期对象（假设时间戳为秒级，如为毫秒级则不需要乘以1000）
                        var date = new Date(value * 1000);
                        var year = date.getFullYear();
                        var month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份从0开始
                        var day = ('0' + date.getDate()).slice(-2);
                        return year + '-' + month + '-' + day;
                    }
                    return '-';
                },
            },
        }
    };
    return Controller;
});
